using System.Diagnostics;

using Common;
using Common.NetWork;

using GrpcClient.Managers;
using GrpcClient.Models;

using GRPCDemo.Protocol;

using XAliphant.Net.Client.Network;

namespace GrpcClient.Services
{
    internal class UserService : Singleton<UserService>
    {
        public IDictionary<string, ResponseCache> MessageCache = new Dictionary<string, ResponseCache>();

        public void Start()
        {
            MessageDistributer.Instance.Subscribe<UserLoginResponse>(OnLoginResponse);
            MessageDistributer.Instance.Subscribe<UserRegistResponse>(OnRegisterResponse);
            MessageDistributer.Instance.Subscribe<UserLoginOutResponse>(OnLoginOutResponse);
        }

        public void Stop()
        {
            MessageDistributer.Instance.Unsubscribe<UserLoginResponse>(OnLoginResponse);
            MessageDistributer.Instance.Unsubscribe<UserRegistResponse>(OnRegisterResponse);
            MessageDistributer.Instance.Unsubscribe<UserLoginOutResponse>(OnLoginOutResponse);
        }


        public string OnLogin(string name, string password)
        {
            NetMessage message = new NetMessage();
            message.Request = new NetMessageRequest();
            message.Request.UserLoginRequest = new UserLoginRequest();
            message.Request.UserLoginRequest.Name = name;
            message.Request.UserLoginRequest.Password = password;

            NetClient.Instance.SendMessage(message);

            string messageId = "OnLogin";
            MessageCache[messageId] = new ResponseCache();
            return messageId;
        }

        private void OnLoginResponse(object sender, UserLoginResponse response)
        {
            string messageId = "OnLogin";
            MessageCache[messageId].HasResponse = true;

            if (response.Result == Result.Success)
            {
                UserManager.Instance.CurrentUser = new User(response.User);
                Debug.WriteLine("登录成功");
            }
            else
            {
                MessageCache[messageId].ErrMsg = response.errMsg;
            }
        }

        public string OnRegister(string name, string password)
        {
            NetMessage message = new NetMessage();
            message.Request = new NetMessageRequest();
            message.Request.UserRegistRequest = new UserRegistRequest();
            message.Request.UserRegistRequest.Name = name;
            message.Request.UserRegistRequest.Password = password;

            NetClient.Instance.SendMessage(message);

            string messageId = "OnRegister";
            MessageCache[messageId] = new ResponseCache();
            return messageId;
        }

        private void OnRegisterResponse(object sender, UserRegistResponse response)
        {
            string messageId = "OnRegister";
            MessageCache[messageId].HasResponse = true;

            if (response.Result == Result.Success)
            {
                UserManager.Instance.CurrentUser = null;
                UserManager.Instance.IsRegister = true;
                Debug.WriteLine("注册成功");
            }
            else
            {
                MessageCache["OnRegister"].ErrMsg = response.errMsg;
            }
        }


        public string OnLoginOut(string name)
        {
            NetMessage message = new NetMessage();
            message.Request = new NetMessageRequest();
            message.Request.UserLoginOutRequest = new UserLoginOutRequest();
            message.Request.UserLoginOutRequest.Name = name;

            NetClient.Instance.SendMessage(message);

            string messageId = "OnLoginOut";
            MessageCache[messageId] = new ResponseCache();
            return messageId;
        }

        private void OnLoginOutResponse(object sender, UserLoginOutResponse response)
        {
            string messageId = "OnLoginOut";
            MessageCache[messageId].HasResponse = true;

            if (response.Result == Result.Success)
            {
                UserManager.Instance.CurrentUser = null;
                Debug.WriteLine("登出成功");
            }
            else
            {
                MessageCache["OnLoginOut"].ErrMsg = response.errMsg;
            }
        }

    }
}
